package com.example.administrator.shzs.fragment.shopdetails;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.example.administrator.shzs.R;
import com.example.administrator.shzs.adapter.ShopDetailAdapter;
import com.example.administrator.shzs.app.MyApplication;
import com.example.administrator.shzs.url.Contents;
import com.example.administrator.shzs.entity.ShopDetailData;
import com.example.administrator.shzs.parse.ParseShopDetail;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 商家详情页面
 */
public class MerchantFragment extends Fragment {


    @Bind(R.id.mXecyclerView)
    RecyclerView mRecyclerView;


    public double latitude;//纬度
    public double longitude;//经度
    private String shopid;
    private String userid;

    private List<ShopDetailData> datas;
    private ShopDetailData data;

    private ShopDetailAdapter adapter;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_merchant, null);

        ButterKnife.bind(this, view);
        setRecyclerView();
        getData();
        getDataFormServer();

        return view;
    }

    private void getData() {
        shopid= MyApplication.app.getShopId();
        userid=MyApplication.app.getUserId();
        latitude=MyApplication.app.getLatitude();
        longitude=MyApplication.app.getLongitude();
    }

    private void getDataFormServer() {


        String url= Contents.shop_detail_url+"?shopid="+shopid+"&userid="+shopid+"&lat="+latitude+"&lng="+longitude;

        RequestParams params=new RequestParams(url);
        Log.e("Life","商家详情"+params.toString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                data= ParseShopDetail.getShopDetailData(result);
                datas=new ArrayList<ShopDetailData>();
                datas.add(data);
                setAdapter();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ex.printStackTrace();
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });

    }

    /**
     * 设置Adapter
     */
    private void setAdapter() {

        adapter=new ShopDetailAdapter(datas,getActivity());
        mRecyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new ShopDetailAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onCallPhoneClick(ShopDetailAdapter.MyViewHolder holder, String shopPhone) {

                if(shopPhone!=null){
                    Intent callPhone = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + shopPhone));
                    callPhone.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(callPhone);
                }

            }
        });
    }





    /**
     * 设置RecyclerView
     */
    private void setRecyclerView() {

        LinearLayoutManager manager=new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayout.VERTICAL);
        mRecyclerView.setLayoutManager(manager);

    }




    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
